﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Logistics.Model;
using Logistics.Service;
using System.Web;
using Logistics.WebApi.Attributes;
using System.Web.Http.Controllers;
using KD.Service.HR.Utility;
using Logistics.Model.Enum;

namespace Logistics.WebApi.Controllers
{
    [MyAuthority]
    public class CompanyController : ApiController
    {
        private CompanyService service = new CompanyService();

        [HttpGet]
        // GET api/<controller>
        public ResponseModel GetCompanyInfo()
        {
            ResponseModel model = new ResponseModel();
            model.InitResultState(StateEnum.Success);
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var companyId = service.GetCompanyIdByAccountId(accountId);
                if(companyId>0)
                {
                    model.mod = service.GetCompanyById(companyId);
                }
                else
                {
                    model.InitResultState(StateEnum.Error);
                }
                
            }
            catch (Exception ex)
            {
                model.InitResultState(ex);
            }

            return model;
        }

        /// <summary>
        /// 获取销售人员列表
        /// </summary>
        /// <returns></returns>
         [HttpPost]
        public ResponseModel GetSaleList()
        {
            ResponseModel reslutmod = new ResponseModel();
            var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
            var companyId = service.GetCompanyIdByAccountId(accountId);
            var saleslist = service.GetSalesByCompanyId(companyId);
            reslutmod.mod = saleslist;
            reslutmod.InitResultState(StateEnum.Success);
            return reslutmod;
        }

        /// <summary>
        /// 添加销售人员
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
         [HttpPost]
        public ResponseModel AddSale(wuliu_company_sales mod)
         {
             ResponseModel reslutmod = new ResponseModel();
             var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
             var companyId = service.GetCompanyIdByAccountId(accountId);
             mod.company_id = companyId;
             var flag = service.AddSale(mod);
             if (flag)
             {
                 reslutmod.InitResultState(StateEnum.Success);
             }
             else
             {
                 reslutmod.InitResultState(StateEnum.Error);
             }
             return reslutmod;
         }

         [HttpPost]
        // POST api/<controller>
        public ResponseModel Add(wuliu_company_base_query company)
        {
            ResponseModel model = new ResponseModel();
            try
            {
                service.AddComapny(company);
                model.InitResultState(StateEnum.Success);
            }
            catch (Exception ex)
            {
                model.InitResultState(ex);
            }
            return model;
        }


        [HttpPost]
        // PUT api/<controller>/5
        public ResponseModel Modify(wuliu_company_base_query company)
        {
            var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
            ResponseModel model = new ResponseModel();
            try
            {
                var companyId = service.GetCompanyIdByAccountId(accountId);
                if (companyId == company.company_id)
                {
                    company.price_mode = 1;
                    service.ModifyComapny(company);
                    model.InitResultState(StateEnum.Success);
                }
                else
                {
                    model.InitResultState(CompanyEnum.MismatchAccount);
                }
            }
            catch (Exception ex)
            {
                model.InitResultState(ex);
            }
            return model;

        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}